package itsm.isperp.module.service.app;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.service.BaseService;
import itsm.isperp.framework.utils.HibernateUUIDGenerator;
import itsm.isperp.module.entity.app.AppUserWebosMenu;
import itsm.isperp.module.repository.app.AppUserWebosMenuMapper;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

/**
 * 
 * 
 * @author lizx
 * @date 2013-10-15
 * @version V1.0
 */
@Service
public class AppUserWebosMenuService extends BaseService<AppUserWebosMenu> {

	@Resource
	protected AppUserWebosMenuMapper appUserWebosMenuMapper;

	@Override
	public AppUserWebosMenuMapper getRepository() {
		return this.appUserWebosMenuMapper;
	}

	public void deleteByUsername(String username) {
		this.getRepository().deleteByUsername(username);
	}

	public void saveBatch(String username, List<AppUserWebosMenu> menus) {
		
		String sql = "INSERT INTO app_user_webos_menu(id,app_menu_id,app_user_name,index_num,order_num)VALUES(?,?,?,?,?)";

		JdbcTemplate jdbcTemplate = ContextHolder
				.getSpringBean("jdbcTemplate");
		List<Object[]> list = new ArrayList<>();

		if (menus != null && menus.size() > 0) {
			for (AppUserWebosMenu m : menus) {
				Object[] arg = new Object[5];
				arg[0] = HibernateUUIDGenerator.generate();
				arg[1] = m.getAppMenuId();
				arg[2] = username;
				arg[3] = m.getIndexNum();
				arg[4] = m.getOrderNum();
				
				list.add(arg);
			}

			jdbcTemplate.batchUpdate(sql, list);
		}
		
	
	}
}
